#include"rclcpp/rclcpp.hpp"
#include"zdys/srv/zdys.hpp"
using zdys::srv::Zdys;
using namespace std::chrono_literals;
class client:public rclcpp::Node{
    private:
    rclcpp::Client<Zdys>::SharedPtr cli;
    public:
    client():Node("client"){
        cli=this->create_client<Zdys>("wbuy");
        RCLCPP_INFO(this->get_logger(),"客服端创建，等待服务器链接");
    }
    bool connect_server(){
        while(!cli->wait_for_service(1s)){
            if(!rclcpp::ok()){
                RCLCPP_INFO(rclcpp::get_logger("rclcpp"),"强制退出");
                return false;
            }
            RCLCPP_INFO(this->get_logger(),"服务链接中，请稍后");
        }
        return true;
    }//链接服务器
    rclcpp::Client<Zdys>::FutureAndRequestId send_request(int32_t jg){
        auto request=std::make_shared<Zdys::Request>();
        request->jg=jg;
        return cli->async_send_request(request);
    }//发送请求
};
int main(int argc,char**argv){
    if(argc!=2){
        RCLCPP_INFO(rclcpp::get_logger("rclcpp"),"请提交一个整形数据");
        return 1;
    }
    rclcpp::init(argc,argv);//初始化
    auto client1=std::make_shared<client>();
    bool flag=client1->connect_server();
    if(!flag){
        RCLCPP_INFO(rclcpp::get_logger("rclcpp"),"服务链接失败");
    }
    auto response=client1->send_request(atoi(argv[1]));
    if(rclcpp::spin_until_future_complete(client1,response)==rclcpp::FutureReturnCode::SUCCESS){
        RCLCPP_INFO(client1->get_logger(),"请求正常处理");
        std::string s;
        if(response.get()->wbuy)  RCLCPP_INFO(client1->get_logger(),"可以购买");
        else RCLCPP_INFO(client1->get_logger(),"不可以购买");
     //接受成功 
    }else{
        RCLCPP_INFO(client1->get_logger(),"请求异常");
    }
    rclcpp::shutdown();
    return 0;
}